package com.study.lihan.letcode;

import com.study.lihan.letcode.util.Util;

import java.util.*;

/*给你一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a，b，c ，使得 a + b + c = 0 ？请你找出所有满足条件且不重复的三元组。
https://leetcode-cn.com/problems/3sum/

* */

/*s思路：转化成两数之和


如何去重
* */
public class My15 {
    //static int[] nums = {-1,0,1,2,-1,-4};
    //static int[] nums = {-2,0,0,2,2};
    static int[] nums = {-2,0,1,1,2};
   // static int[] nums = {-2,0,1,1,2};
   //static int[] nums = {1,1,-2};
    public static void main(String[] args) {
       // List<List<Integer>> lists = mythreeSum(nums);
        List<List<Integer>> lists = threeSum(nums);
        System.out.println("结果是："+lists);

    }
    public static List<List<Integer>> mythreeSum(int[] nums) {//有问题
        if(nums==null || nums.length<3)return  new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        int arrLen = nums.length;
        int num40=0;
        for(int i= 0; i<arrLen; i++){
            if(nums[i]>0)break;
            if(nums[i]==0)num40++;
           //a元素去重
            if(i>0 && nums[i]==nums[i-1])continue;

            //第二轮循环应有的值
            int tempResult = -nums[i];
            Set<Integer> twoSumSet = new HashSet<>();
            for(int j=i+1; j<arrLen;j++ ){
                if(nums[i]==nums[j]&& nums[i]==0)continue;
                if(i>0 && nums[j]==nums[j-1] &&  nums[j]==nums[j-2])continue;
                if(twoSumSet.contains(nums[j])) {
                    List<Integer> arr = new ArrayList<>(3);
                    arr.add(nums[i]);
                    arr.add(nums[j]);
                    arr.add(-nums[i] - nums[j]);
                    result.add(arr);
                }
                twoSumSet.add(tempResult-nums[j]);
            }
        }
       if(num40/3 !=0){
            List<Integer>  arr = new ArrayList<>(3);
            arr.add(0);
            arr.add(0);
            arr.add(0);
            result.add(arr);
        }

        return result;
    }

    public static  List<List<Integer>> threeSum(int[] nums) {
        if(nums==null || nums.length<3)return  new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        int arrLen= nums.length;
        for(int i=0; i<arrLen; i++){
            if (nums[i] > 0) {
                return result;
            }

            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            int left = i+1;
            int right = arrLen -1;
           while (right >left){
               int sum = nums[i]+ nums[left]+ nums[right];
               if(sum >0){
                   right--;
                   continue;
               };
               if(sum < 0){
                   left++;
                   continue;
               }
               result.add(Arrays.asList(nums[i], nums[left], nums[right]));
               right--;
               left++;
               while (right > left && nums[right] == nums[right + 1]) right--;
               while (right > left && nums[left] == nums[left - 1]) left++;


           }

        }
      return result;
    }
}
